********************************************************************************
** Regression dofile "Mums Go Online: Is the Internet Changing the Demand 
** for Healthcare?" by Amaral-Garcia, Nardotto, Propper and Valletti.
********************************************************************************


clear *

cap log close

cap cd "C:\HealthInternet_UKdata"

global ppath "C:\HealthInternetREStat"
global tablepath "C:\HealthInternetREStat\Tables"
global extradatapath "C:\HealthInternetREStat\ExtraData"

use FinalData\delivery_main.dta, clear

* Set as cov1 the following list of vars:
#delimit ;
global cov1="
agedum2 agedum3 agedum4 agedum5 agedum6 anemia breech cardiaclung cordprol 
diabetes disprop distress herpes hypereclamp cervix obesity  
placprevia prematmem previousCS prevabortion renal rh twins white";
#delimit cr

global cov2="$cov1 newid_2_trend-newid_1209_trend"

***************************************************************** 
* Table 2: Regressions C-section model.
* Version with LSOA pairs FE interacted with trend and hosp FE
*****************************************************************

estimates clear
eststo clear

areg csec did d2006 i.hesyear $cov1 imd04ed imd04i dist_hosp ///
newid_2_trend-newid_1209_trend i.hospnr, cluster(newid) a(id)
est store reg1

areg csec did d2006 i.hesyear $cov1 imd04ed imd04i dist_hosp ///
newid_2_trend-newid_1209_trend i.hospnr if experienced==1, cluster(newid) a(id)
est store reg2

areg csec did d2006 i.hesyear $cov1 imd04ed imd04i dist_hosp ///
newid_2_trend-newid_1209_trend i.hospnr if experienced==0, cluster(newid) a(id)
est store reg3

areg csec did d2006 i.hesyear $cov1 imd04ed imd04i dist_hosp ///
newid_2_trend-newid_1209_trend i.hospnr if del_csecemerg==0, cluster(newid) a(id)
est store reg4

areg csec did d2006 i.hesyear $cov1 imd04ed imd04i dist_hosp ///
newid_2_trend-newid_1209_trend i.hospnr if experienced==1 & del_csecemerg==0, cluster(newid) a(id)
est store reg5

areg csec did d2006 i.hesyear $cov1 imd04ed imd04i dist_hosp ///
newid_2_trend-newid_1209_trend i.hospnr if experienced==0 & del_csecemerg==0, cluster(newid) a(id)
est store reg6

areg del_csecemerg did d2006 i.hesyear $cov1 imd04ed imd04i dist_hosp ///
newid_2_trend-newid_1209_trend i.hospnr, cluster(newid) a(id)
est store reg7

#delimit ;
estout reg1 reg2 reg3 reg4 reg5 reg6 reg7 using $tablepath\Csections.tex, 
cells(b(fmt(2) star) se(par fmt(2))) style(tex) 
starlevels(* 0.1 ** 0.05 *** 0.01) 
coll(,none) ml(,none)  
prehead(\begin{tabular}{lccccccc}\hline\hline
\textbf{Dependent} & \multicolumn{6}{c}{\textbf{Probability of c-section}}				   & \textbf{Probability Emer-} \\
\textbf{variable:} & \multicolumn{6}{c}{}  				 								   & \textbf{gency c-section} \\
				  & \multicolumn{3}{c}{All deliv. types} & \multicolumn{3}{c}{Vag del and} & All deliv.\\
				  & \multicolumn{3}{c}{} 				 & \multicolumn{3}{c}{Elective C-sec} & types\\
\cmidrule(l{4pt}r{4pt}){2-4}\cmidrule(l{4pt}r{4pt}){5-7}\cmidrule(l{4pt}r{4pt}){8-8}
& All & Mult- & First- & All & Mult-  & First- & All   \\
& 	  & time  & time   &     & -time  & -time  & 	  \\
&(1)&(2)&(3)&(4)&(5)&(6)&(7)\\ 
\cmidrule(l{4pt}r{4pt}){2-2}\cmidrule(l{4pt}r{4pt}){3-3}
\cmidrule(l{4pt}r{4pt}){4-4}\cmidrule(l{4pt}r{4pt}){5-5}
\cmidrule(l{4pt}r{4pt}){6-6}\cmidrule(l{4pt}r{4pt}){7-7}
\cmidrule(l{4pt}r{4pt}){8-8})
prefoot(Controls & Yes & Yes & Yes & Yes & Yes & Yes & Yes \\\hline)
postfoot(\hline\hline\end{tabular})
keep(did)
order(did)
stats(r2 N, labels("R$^2$" "Observations") fmt(3 0)) varlabels(_cons Constant) 
label replace;
#delimit cr


***************************************************
* Table 3: Mothers' outcomes 
***************************************************
estimates clear
eststo clear

preserve
foreach variab in induction epis ccmorb prolonglab prolongpg trvag {
	replace `variab'=`variab'*100
}
* Note: morb is already 1 or 100

* Induction of Labor
areg induction did d2006 i.hesyear $cov1 imd04ed imd04i dist_hosp ///
newid_2_trend-newid_1209_trend i.hospnr, cluster(newid) a(id)
eststo reg1

* Episiotomy
areg epis did d2006 i.hesyear $cov1 imd04ed imd04i dist_hosp ///
newid_2_trend-newid_1209_trend i.hospnr if csec==0, cluster(newid) a(id)
eststo reg2

* Co-mordibities
areg morb did d2006 i.hesyear $cov1 imd04ed imd04i dist_hosp ///
newid_2_trend-newid_1209_trend i.hospnr, cluster(newid) a(id)
eststo reg3

* Anaesthetic during labor
areg anesth did d2006 i.hesyear $cov1 imd04ed imd04i dist_hosp ///
i.hospnr, cluster(newid) a(id)
eststo reg4

* Prolonged Labour
* The regression does not use $cov1 because prolonged labour is included in cov1
areg prolonglab did d2006 i.hesyear ///
agedum2 agedum3 agedum4 agedum5 agedum6 anemia breech cardiaclung cordprol /// 
diabetes disprop distress herpes hypereclamp cervix obesity  /// 
placprevia prematmem previousCS prevabortion renal rh twins white /// 
imd04ed imd04i newid_2_trend-newid_1209_trend i.hospnr, cluster(newid) a(id)
eststo reg5

* Prolonged pregnancy
areg prolongpg did d2006 i.hesyear ///
agedum2 agedum3 agedum4 agedum5 agedum6 anemia breech cardiaclung cordprol /// 
diabetes disprop distress herpes hypereclamp cervix obesity  /// 
placprevia prematmem previousCS prevabortion renal rh twins white /// 
imd04ed imd04i newid_2_trend-newid_1209_trend i.hospnr, cluster(newid) a(id)
eststo reg6

* Vaginal trauma
areg trvag did d2006 i.hesyear $cov1 imd04ed imd04i /// 
newid_2_trend-newid_1209_trend i.hospnr if csec==0, cluster(newid) a(id)
eststo reg7 

restore

#delimit ;
estout reg1 reg2 reg3 reg4 reg5 reg6 reg7
using $tablepath\MothersOutcomes.tex, 
cells(b(fmt(2) star) se(par fmt(2))) style(tex) 
starlevels(* 0.1 ** 0.05 *** 0.01) 
coll(,none) ml(,none)  
prehead(\begin{tabular}{lccccccc}\hline\hline 
\multicolumn{6}{l}{\textbf{Dependent variable: Procedure and outcome}}\\
 & Induction & Episiotomy & Co-mor- & Anaesthetic & Prolonged & Prolonged & Maternal \\
  &  &  & bidities &  & Labor & Pregnancy & Trauma \\
 &(1)&(2)&(3)&(4)&(5)&(6)&(7)\\ 
 \cmidrule(l{4pt}r{4pt}){2-2}\cmidrule(l{4pt}r{4pt}){3-3} 
 \cmidrule(l{4pt}r{4pt}){4-4}\cmidrule(l{4pt}r{4pt}){5-5} 
 \cmidrule(l{4pt}r{4pt}){6-6}\cmidrule(l{4pt}r{4pt}){7-7}
\cmidrule(l{4pt}r{4pt}){8-8})
prefoot(Controls  & Yes & Yes & Yes & Yes & Yes	& Yes& Yes\\\hline)
postfoot(\hline\hline\end{tabular})
keep(did )
order(did )
stats(r2 N, labels("R$^2$" "Observations") fmt(3 0)) varlabels(_cons Constant) 
label replace;
#delimit cr


************************************
* Table 4: Newborns
************************************

estimates clear
eststo clear

gen notdischar=1 if dischar==0
replace notdischar=0 if dischar==1

preserve
foreach variab in preemie lbw distress stillbirth resus notdischar {
	replace `variab'=`variab'*100
}

* Preemies
areg preemie did d2006 hesyear $cov1 imd04ed imd04i dist_hosp ///
 i.hospnr, cluster(newid) a(id)
eststo reg1  // Otherwise matrix highly singular

* Low birth weigh 
areg lbw did d2006 hesyear $cov1 gestage imd04ed imd04i dist_hosp ///
newid_2_trend-newid_1209_trend i.hospnr, cluster(newid) a(id)
eststo reg2

* Fetal distress
areg distress did d2006 hesyear ///
agedum2 agedum3 agedum4 agedum5 agedum6 anemia breech cardiaclung cordprol /// 
diabetes disprop       herpes hypereclamp cervix obesity  /// 
placprevia prematmem previousCS prevabortion renal rh twins white /// 
imd04ed imd04i dist_hosp ///
newid_2_trend-newid_1209_trend i.hospnr, cluster(newid) a(id)
eststo reg3

* Stillbirth
areg stillbirth did d2006 hesyear $cov1 imd04ed imd04i dist_hosp ///
newid_2_trend-newid_1209_trend i.hospnr, cluster(newid) a(id)
eststo reg4

* Resuscitation
areg resus did d2006 hesyear $cov1 imd04ed imd04i dist_hosp ///
 i.hospnr, cluster(newid) a(id)
eststo reg5

* Discharged vs died in hospital & stayed in hospital 
areg notdischar did d2006 hesyear $cov1 imd04ed imd04i dist_hosp ///
newid_2_trend-newid_1209_trend i.hospnr, cluster(newid) a(id)
eststo reg6

restore

#delimit ;
estout reg1 reg2 reg3 reg4 reg5 reg6
using $tablepath\NewbornsOutcomes.tex, 
cells(b(fmt(2) star) se(par fmt(2))) style(tex) 
starlevels(* 0.1 ** 0.05 *** 0.01) 
coll(,none) ml(,none)  
prehead(\begin{tabular}{lcccccc}\hline\hline
\textbf{Dep var:}  & Premature & Low Birth Weight & Fetal Distress & Stillbirth & Resusc & Died/Stay Hosp \\
&(1)&(2)&(3)&(4)&(5)\\ 
\cmidrule(l{4pt}r{4pt}){2-2}\cmidrule(l{4pt}r{4pt}){3-3}
\cmidrule(l{4pt}r{4pt}){4-4}\cmidrule(l{4pt}r{4pt}){5-5}
\cmidrule(l{4pt}r{4pt}){6-6}\cmidrule(l{4pt}r{4pt}){7-7})
prefoot(Controls & Yes & Yes & Yes & Yes & Yes& Yes \\\hline)
postfoot(\hline\hline\end{tabular})
keep(did)
order(did)
stats(r2 N, labels("R$^2$" "Observations") fmt(3 0)) varlabels(_cons Constant) 
label replace;
#delimit cr

estimates clear
eststo clear

* Table 5: see end o f file

*****************************************************
* Table 6: Heterogenous effects by Income & Education
* First-time mothers.
*****************************************************

estimates clear
eststo clear

areg csec did d2006 i.hesyear $cov1 dist_hosp i.hospnr if high_incmed==1 & experienced==0, cluster(newid) a(id)
est store reg1  // Otherwise matrix highly singular

areg csec did d2006 i.hesyear $cov1 dist_hosp i.hospnr if high_incmed==0 & experienced==0, cluster(newid) a(id)
est store reg2

areg csec did d2006 i.hesyear $cov1 dist_hosp i.hospnr if high_educmed==1 & experienced==0, cluster(newid) a(id)
est store reg3

areg csec did d2006 i.hesyear $cov1 dist_hosp i.hospnr if high_educmed==0 & experienced==0, cluster(newid) a(id)
est store reg4


#delimit ;
estout reg1 reg2 reg3 reg4 
using $tablepath\HeterogEffects.tex, 
cells(b(fmt(2) star) se(par fmt(2))) style(tex) 
starlevels(* 0.1 ** 0.05 *** 0.01) 
coll(,none) ml(,none)  
prehead(\begin{tabular}{lcccc}\hline\hline
Dep var: & \multicolumn{4}{c}{C-sec}    \\
 & High Inc & Low Inc & High Educ & Low Educ \\ 
 &(1)&(2)&(3)&(4)\\ 
\cmidrule(l{4pt}r{4pt}){2-2}\cmidrule(l{4pt}r{4pt}){3-3}
\cmidrule(l{4pt}r{4pt}){4-4}\cmidrule(l{4pt}r{4pt}){5-5})
prefoot(Controls & Yes & Yes & Yes & Yes \\\hline)
postfoot(\hline\hline\end{tabular})
keep(did)
order(did)
stats(r2 N, labels("R$^2$" "Observations") fmt(3 0)) varlabels(_cons Constant) 
label replace;
#delimit cr


********************************************************************************
* Appendix: Heterogeneous effect of the internet on C-sections—Other results
********************************************************************************  

*****************************************************
* Table 8: Regressions mother’s outcomes - 
* low educated mothers
*****************************************************

estimates clear
eststo clear

* Light version
preserve
foreach variab in induction epis ccmorb prolonglab prolongpg trvag{
	replace `variab'=`variab'*100
}
* Note: morb is already 1 or 100

* Induction of Labor
areg induction did d2006 i.hesyear $cov1 imd04ed imd04i dist_hosp i.hospnr if high_educ==0, cluster(newid) a(id)
eststo reg1

* Episiotomy
areg epis did d2006 i.hesyear $cov1 imd04ed imd04i dist_hosp i.hospnr if csec==0 & high_educ==0, cluster(newid) a(id)
eststo reg2

* Co-mordibities
areg morb did d2006 i.hesyear $cov1 imd04ed imd04i dist_hosp i.hospnr if high_educ==0, cluster(newid) a(id)
eststo reg3

* Anaesthetic during labor
areg anesth did d2006 i.hesyear $cov1 imd04ed imd04i dist_hosp i.hospnr if high_educ==0, cluster(newid) a(id)
eststo reg4

* Prolonged Labour
areg prolonglab did d2006 i.hesyear ///
agedum2 agedum3 agedum4 agedum5 agedum6 anemia breech cardiaclung cordprol /// 
diabetes disprop distress herpes hypereclamp cervix obesity  /// 
placprevia prematmem previousCS prevabortion renal rh twins white /// 
imd04ed imd04i dist_hosp i.hospnr if high_educ==0, cluster(newid) a(id)
eststo reg5

* Prolonged pregnancy
areg prolongpg did d2006 i.hesyear ///
agedum2 agedum3 agedum4 agedum5 agedum6 anemia breech cardiaclung cordprol /// 
diabetes disprop distress herpes hypereclamp cervix obesity  /// 
placprevia prematmem previousCS prevabortion renal rh twins white /// 
imd04ed imd04i dist_hosp i.hospnr if high_educ==0, cluster(newid) a(id)
eststo reg6

* Vaginal Trauma/Maternal tears
areg trvag did d2006 i.hesyear $cov1 imd04ed imd04i dist_hosp i.hospnr if csec==0 & high_educ==0, cluster(newid) a(id)
eststo reg7

restore

#delimit ;
estout reg1 reg2 reg3 reg4 reg5 reg6 reg7
using $tablepath\MothersOutcomesLowEduc.tex, 
cells(b(fmt(2) star) se(par fmt(2))) style(tex) 
starlevels(* 0.1 ** 0.05 *** 0.01) 
coll(,none) ml(,none)  
prehead(\begin{tabular}{lccccccc}\hline\hline 
\multicolumn{6}{l}{\textbf{Dependent variable: Procedure and outcome}}\\
 & Induction & Episiotomy & Co-mor- & Anaesthetic & Prolonged 
 & Prolonged & Maternal \\
  &  &  & bidities &  & Labor
 & Pregnancy & Trauma \\
 &(1)&(2)&(3)&(4)&(5)&(6)&(7)\\ 
 \cmidrule(l{4pt}r{4pt}){2-2}\cmidrule(l{4pt}r{4pt}){3-3} 
 \cmidrule(l{4pt}r{4pt}){4-4}\cmidrule(l{4pt}r{4pt}){5-5} 
 \cmidrule(l{4pt}r{4pt}){6-6}\cmidrule(l{4pt}r{4pt}){7-7}
\cmidrule(l{4pt}r{4pt}){8-8})
prefoot(Controls   & Yes & Yes & Yes & Yes & Yes	& Yes& Yes\\\hline)
postfoot(\hline\hline\end{tabular})
keep(did )
order(did )
stats(r2 N, labels("R$^2$" "Observations") fmt(3 0)) varlabels(_cons Constant) 
label replace;
#delimit cr


*****************************************************
* Table 9: Newborns’ outcomes - low educated mothers.
*****************************************************

estimates clear

preserve
foreach variab in preemie lbw distress stillbirth resus dischar {
	replace `variab'=`variab'*100
}

* Preemies
areg preemie did d2006 hesyear $cov1 imd04ed imd04i dist_hosp if high_educ==0, cluster(newid) a(id)
eststo reg1

* Birth weigh
areg lbw did d2006 hesyear $cov1 gestage imd04ed imd04i dist_hosp if high_educ==0, cluster(newid) a(id)
eststo reg2

* Fetal distress
areg distress did d2006 hesyear ///
agedum2 agedum3 agedum4 agedum5 agedum6 anemia breech cardiaclung cordprol /// 
diabetes disprop       herpes hypereclamp cervix obesity  /// 
placprevia prematmem previousCS prevabortion renal rh twins white /// 
imd04ed imd04i dist_hosp if high_educ==0, cluster(newid) a(id)
eststo reg3

* Stillbirth
areg stillbirth did d2006 hesyear $cov1 imd04ed imd04i dist_hosp if high_educ==0, cluster(newid) a(id)
eststo reg4

* Resuscitation
areg resus did d2006 hesyear $cov1 imd04ed imd04i dist_hosp if high_educ==0, cluster(newid) a(id)
eststo reg5

* Discharged vs died in hospital & stayed in hospital - remeber this is for mothers
areg dischar did d2006 hesyear $cov1 imd04ed imd04i dist_hosp if high_educ==0, cluster(newid) a(id)
eststo reg6

restore

#delimit ;
estout reg1 reg2 reg3 reg4 reg5 reg6
using $tablepath\NewbornsOutcomesLowEduc.tex, 
cells(b(fmt(2) star) se(par fmt(2))) style(tex) 
starlevels(* 0.1 ** 0.05 *** 0.01) 
coll(,none) ml(,none)  
prehead(\begin{tabular}{lcccccc}\hline\hline
\textbf{Dep var:}  & Premature & Low Birth Weight & Fetal Distress & Stillbirth & Resusc & Discharged \\
&(1)&(2)&(3)&(4)&(5)&(6)\\ 
\cmidrule(l{4pt}r{4pt}){2-2}\cmidrule(l{4pt}r{4pt}){3-3}
\cmidrule(l{4pt}r{4pt}){4-4}\cmidrule(l{4pt}r{4pt}){5-5}
\cmidrule(l{4pt}r{4pt}){6-6}\cmidrule(l{4pt}r{4pt}){7-7})
prefoot(Controls & Yes & Yes & Yes & Yes & Yes& Yes \\\hline)
postfoot(\hline\hline\end{tabular})
keep(did)
order(did)
stats(r2 N, labels("R$^2$" "Observations") fmt(3 0)) varlabels(_cons Constant) 
label replace;
#delimit cr

estimates clear
eststo clear



*************************************************
* Table 5. Internet technology and C-sections
*************************************************

use FinalData\FinalDeliveryForSpeedRegressions.dta, clear

gen noexp=experienced==0
gen noexp_speed=noexp*speed

areg csec speed i.hesyear i.hospnr $cov2 imd04ed imd04i dist_hosp, cluster(newid) a(id)
est store speed1

areg csec speed i.hesyear i.hospnr $cov2 imd04ed imd04i dist_hosp if experienced==1, cluster(newid) a(id)
est store speed2

areg csec speed i.hesyear i.hospnr $cov2 imd04ed imd04i dist_hosp if experienced==0, cluster(newid) a(id)
est store speed3

areg csec speed i.hesyear i.hospnr $cov2 imd04ed imd04i dist_hosp if del_csecemerg==0, cluster(newid) a(id)
est store speed4

areg csec speed i.hesyear i.hospnr $cov2 imd04ed imd04i dist_hosp if experienced==1 & del_csecemerg==0, cluster(newid) a(id)
est store speed5

areg csec speed i.hesyear i.hospnr $cov2 imd04ed imd04i dist_hosp if experienced==0 & del_csecemerg==0, cluster(newid) a(id)
est store speed6

areg del_csecemerg speed i.hesyear i.hospnr $cov2 imd04ed imd04i dist_hosp, cluster(newid) a(id)
est store speed7

#delimit ;
estout speed1 speed2 speed3 speed4 speed5 speed6 speed7
using $tablepath\SpeedRegs.tex, 
cells(b(fmt(2) star) se(par fmt(2))) style(tex) 
starlevels(* 0.1 ** 0.05 *** 0.01) 
coll(,none) ml(,none)  
prehead(\begin{tabular}{lccccccc|c}\hline\hline
\multicolumn{1}{l}{\textbf{Dependent}} & \multicolumn{7}{c}{\textbf{Probability of c-section}} 
& \multicolumn{1}{c}{\textbf{Probability Emer-}} \\
\multicolumn{1}{l}{\textbf{variable:}} & \multicolumn{7}{c}{} 
& \multicolumn{1}{c}{\textbf{gency c-section}} \\
& \multicolumn{3}{c}{All deliv. types} & \multicolumn{4}{c}{Vag del and} & All deliv.\\
& \multicolumn{3}{c}{} & \multicolumn{4}{c}{Elective C-sec} & types\\
\cmidrule(l{4pt}r{4pt}){2-4}\cmidrule(l{4pt}r{4pt}){5-8}\cmidrule(l{4pt}r{4pt}){9-9}
& All & Mult- & First- & All & Mult-  & First- & All & All  \\
& 	  & time  & time   &     & -time  & -time  & 	 & \\
&(1)&(2)&(3)&(4)&(5)&(6)&(7)&(8)\\ 
\cmidrule(l{4pt}r{4pt}){2-2}\cmidrule(l{4pt}r{4pt}){3-3}
\cmidrule(l{4pt}r{4pt}){4-4}\cmidrule(l{4pt}r{4pt}){5-5}
\cmidrule(l{4pt}r{4pt}){6-6}\cmidrule(l{4pt}r{4pt}){7-7}
\cmidrule(l{4pt}r{4pt}){8-8}\cmidrule(l{4pt}r{4pt}){9-9})
prefoot(Controls & Yes & Yes & Yes & Yes & Yes & Yes & Yes & Yes\\\hline)
postfoot(\hline\hline\end{tabular})
keep(speed experienced) 
order(speed experienced )
stats(r2 N, labels("R$^2$" "Observations") fmt(3 0)) varlabels(_cons Constant) 
label replace;
#delimit cr

eststo clear


*++++++++++++++++++++++++ Robustness Age +++++++++++++++++++++++++++++++++++++++
use D:\HealthUK\delivery_matched_full_postmatch.dta, clear

gen elec=del_csecelec
la var elec "Elective C-sec"

gen emerg=del_csecemerg
la var emerg "Emergency C-sec"

cap drop prevpreg
	gen prevpreg=0 if numpreg==0
	replace prevpreg=1 if numpreg>0 & numpreg!=99
	foreach lname in Z340 Z355 Z356{
			replace prevpreg = 0 if prevpreg==. & (diag3_01 == "`lname'" | diag4_01 == "`lname'")		
			replace prevpreg = 0 if prevpreg==. & (diag_1 == "`lname'" | diag_2 == "`lname'" | diag_3 == "`lname'" | diag_4 == "`lname'" | diag_5 == "`lname'" | diag_6 == "`lname'" | diag_7 == "`lname'" | diag_8 == "`lname'" | diag_9 == "`lname'" | diag_10 == "`lname'")
	}
	replace prevpreg=1 if vbac==1 & prevpreg==.
	replace prevpreg=1 if previousCS==1 & prevpreg==.

gen experienced=prevpreg 
la var experienced "Multiple-time"

gen expdid=experienced*did
la var expdid "Multiple $\times$ Did"

gen expd2006=experienced*d2006

replace previousCS=0 if experienced==0

* income deprivation (mean/median-split) - see part on heterogeneities
sum imd04i, d
gen high_inc = (imd04i < r(mean))
la var high_inc "High Income"

gen highinc_did=high_inc*did
la var highinc_did "High Inc $\times$ Did"

gen highinc2006=high_inc*2006

sum imd04i, d
gen high_incmed = (imd04i < r(p50))
la var high_incmed "High Income-median"

gen highincmed_did=high_incmed*did
la var highincmed_did "High Inc $\times$ Did"

gen highincmed2006=high_incmed*2006

* education deprivation (mean/median-split) - see part on heterogeneities
sum imd04ed, d
gen high_educ = (imd04ed < r(mean))
la var high_educ "High Education"

gen higheduc_did=high_educ*did
la var higheduc_did "High Educ $\times$ Did"

gen higheduc2006=high_educ*2006

sum imd04ed, d
gen high_educmed = (imd04ed < r(p50))
la var high_educmed "High Educ-median"

gen higheducmed_did=high_educmed*did
la var higheducmed_did "High Educ $\times$ Did"

gen highinceduc2006=high_educmed*2006

#delimit ;
global cov1="
agedum2 agedum3 agedum4 agedum5 agedum6 anemia breech cardiaclung cordprol 
diabetes disprop distress herpes hypereclamp cervix obesity  
placprevia prematmem previousCS prevabortion renal rh twins white";
#delimit cr

* Generate the fixed effects
qui tab newid, gen(newid_)
forval i=1/1209 {
	gen newid_`i'_trend=newid_`i'*hesyear
}

global cov2="$cov1 newid_2_trend-newid_1209_trend"


egen hospnr=group(procode) // We need it for hospitals' FEs

* Deliveries per year
sort hesyear period

***************************************
* Table 7: Regressions C-section model 
* re-balancing mothers’ age.
***************************************

estimates clear
eststo clear
areg csec did d2006 i.hesyear $cov2 imd04ed imd04i dist_hosp i.hospnr, cluster(newid) a(id)
est store reg1

areg csec did d2006 i.hesyear $cov2 imd04ed imd04i dist_hosp i.hospnr if experienced==1, cluster(newid) a(id)
est store reg2

areg csec did d2006 i.hesyear $cov2 imd04ed imd04i dist_hosp i.hospnr if experienced==0, cluster(newid) a(id)
est store reg3

areg csec did d2006 i.hesyear $cov2 imd04ed imd04i dist_hosp i.hospnr if del_csecemerg==0, cluster(newid) a(id)
est store reg4

areg csec did d2006 i.hesyear $cov2 imd04ed imd04i dist_hosp i.hospnr if experienced==1 & del_csecemerg==0, cluster(newid) a(id)
est store reg5

areg csec did d2006 i.hesyear $cov2 imd04ed imd04i dist_hosp i.hospnr if experienced==0 & del_csecemerg==0, cluster(newid) a(id)
est store reg6

areg del_csecemerg did d2006 i.hesyear $cov2 imd04ed imd04i dist_hosp i.hospnr, cluster(newid) a(id)
est store reg7

#delimit ;
estout reg1 reg2 reg3 reg4 reg5 reg6 reg7 using $tablepath\CsectionsRobAge.tex, 
cells(b(fmt(2) star) se(par fmt(2))) style(tex) 
starlevels(* 0.1 ** 0.05 *** 0.01) 
coll(,none) ml(,none)  
prehead(\begin{tabular}{lccccccc}\hline\hline
\textbf{Dependent} & \multicolumn{6}{c}{\textbf{Probability of c-section}}				   & \textbf{Probability Emer-} \\
\textbf{variable:} & \multicolumn{6}{c}{}  				 								   & \textbf{gency c-section} \\
				  & \multicolumn{3}{c}{All deliv. types} & \multicolumn{3}{c}{Vag del and} & All deliv.\\
				  & \multicolumn{3}{c}{} 				 & \multicolumn{3}{c}{Elective C-sec} & types\\
\cmidrule(l{4pt}r{4pt}){2-4}\cmidrule(l{4pt}r{4pt}){5-7}\cmidrule(l{4pt}r{4pt}){8-8}
& All & Mult- & First- & All & Mult-  & First- & All   \\
& 	  & time  & time   &     & -time  & -time  & 	  \\
&(1)&(2)&(3)&(4)&(5)&(6)&(7)\\ 
\cmidrule(l{4pt}r{4pt}){2-2}\cmidrule(l{4pt}r{4pt}){3-3}
\cmidrule(l{4pt}r{4pt}){4-4}\cmidrule(l{4pt}r{4pt}){5-5}
\cmidrule(l{4pt}r{4pt}){6-6}\cmidrule(l{4pt}r{4pt}){7-7}
\cmidrule(l{4pt}r{4pt}){8-8})
prefoot(Controls & Yes & Yes & Yes & Yes & Yes & Yes & Yes \\\hline)
postfoot(\hline\hline\end{tabular})
keep(did)
order(did)
stats(r2 N, labels("R$^2$" "Observations") fmt(3 0)) varlabels(_cons Constant) 
label replace;
#delimit cr

cap log close


